#pragma once

#include <iostream>
#include "udp_sock.hpp"

class UdpClient
{
public:
    UdpClient(std::string ip, uint16_t port)
        : _ip(ip), _port(port), _sock()
    {
    }
    ~UdpClient() {}

    void Init()
    {
        _sock.Socket();
    }

    void Start()
    {
        while (true)
        {
            // 1. 发送数据
            std::string msg;
            std::cout << "Please Enter# ";
            std::cin >> msg;
            struct sockaddr_in peer;
            memset(&peer, 0, sizeof(peer));
            peer.sin_family = AF_INET;
            peer.sin_port = htons(_port);
            peer.sin_addr.s_addr = inet_addr(_ip.c_str());
            _sock.Send(msg, peer, sizeof(peer));
            // 2. 接受数据
            std::string echo_msg;
            socklen_t len = sizeof(peer);
            _sock.Recv(&echo_msg, &peer,&len);
            // 3. 显示数据
            std::cout << "server echo# " << echo_msg << std::endl;
        }
    }

private:
    UdpSock _sock;
    uint16_t _port;
    std::string _ip;
};